<template>
  <div id="admin-manage" class="page-wrapper">
    <!-- <h1 class="main-title">搜索设备</h1> -->
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <el-form class="form" :inline="true" ref="form" :model="selectorData">
          <GroupSelector :selectorData="selectorData" />
        </el-form>
      </div>
      <el-button style="text-align: right;" type="primary" @click="onSubmit"
        >获取数据</el-button
      >
    </div>

    <!-- 表格 -->
    <!-- <h1 class="main-title">已有设备列表</h1> -->
    <div class="wrapper main-card">
      <div>
        <el-table
          :cell-style="{ textAlign: 'center' }"
          :header-cell-style="{ 'text-align': 'center' }"
          :data="pageData"
          style="width: 100%"
        >
          <el-table-column prop="master_id" label="总控编号"> </el-table-column>
          <el-table-column prop="region" label="楼栋"> </el-table-column>
          <el-table-column prop="layer" label="楼层"> </el-table-column>
          <el-table-column label="电器状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                disabled
                active-value="1"
                inactive-value="0"
                @change="handleSwitchChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="buildingId" label="收费模板">
          </el-table-column>
          <el-table-column prop="buildingId" label="补助模板">
          </el-table-column>
          <el-table-column
            prop="meterRead"
            label="电表读数(度)"
          ></el-table-column>
          <el-table-column
            prop="lastRead"
            label="上次电表读数(度)"
          ></el-table-column>
          <el-table-column label="用表日期">
            <template slot-scope="scope">
              {{ formatDate(scope.row.updatedAt) }}
            </template>
          </el-table-column>
          <el-table-column property="button" label="操作" width="180px">
            <template slot-scope="scope">
              <el-button>抄表</el-button>
              <el-button @click="modify(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 表格 -->
    <el-dialog
      title="参数详情"
      :visible.sync="dialogVisible"
      width="800px"
      height="800px"
    >
      <el-tabs type="border-card">
        <el-form class="form" ref="form" :model="modifydata">
          <div class="form-item-wrapper">
            <el-form-item label="设备id" required prop="id">
              <el-input disabled v-model.trim="modifydata.id"></el-input>
            </el-form-item>
            <el-form-item label="开关状态" required prop="state">
              <template>
                <el-switch
                  v-model.trim="modifydata.state"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="0"
                >
                </el-switch>
              </template>
            </el-form-item>
          </div>
        </el-form>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import GroupSelector from './components/all-device.vue'
import { addmasters } from '@/api/meter'
import { getmasters, upmaster } from '@/api/equipment'
export default {
  name: 'RoomInfo',
  components: {
    GroupSelector
  },
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      dialogVisible: false,
      selectorData: {
        master_id: null,
        buildingId: null,
        floorId: null
      },
      modifydata: {},
      tableData: []
    }
  },
  mounted() {
    // this.fetchData()
  },
  methods: {
    formatDate(isoString) {
      const date = new Date(isoString)
      const options = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      }
      return date.toLocaleString('zh-CN', options)
    },
    //修改
    modify(row) {
      console.log('row', row)
      this.modifydata = { ...row } // 复制当前行的数据到 modifydata
      this.dialogVisible = true
    },
    handleSubmit() {
      upmaster(this.modifydata).then(res => {
        if (res.success) {
          this.$message.success('修改成功')
          const index = this.tableData.findIndex(
            item => item.id === this.modifydata.id
          )
          if (index !== -1) {
            this.tableData.splice(index, 1, { ...this.modifydata })
          }
          this.dialogVisible = false
        } else {
          this.$message.error('修改失败')
        }
      })
    },
    // 分页逻辑
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = this.currentPage * this.pageSize
      console.log('切割数据', this.tableData.slice(start, end))
      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentPage = val
      this.pageData = this.queryByPage()
    },
    // 获取服务器数据
    fetchData() {
      getmasters().then(res => {
        this.tableData = res.data.master
        this.total = res.data.master.length // 设置总数据量
        this.pageData = this.queryByPage() // 初始化分页数据
      })
    },
    onSubmit() {
      console.log(this.selectorData)
      getmasters(this.selectorData).then(res => {
        console.log(res)
        this.tableData = res.data.master
        this.total = res.data.master.length // 设置总数据量
        this.pageData = this.queryByPage() // 初始化分页数据
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}

.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 垂直居中 */
}

.list-wrapper {
  margin-top: 40px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}

.form-item-wrapper {
  display: flex;
  justify-content: space-around;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  /* Adjust margin as needed */
}
</style>
